from django.shortcuts import render
import json
from django.http import JsonResponse
from user.models import User
from util.hash_tools import md5_string
from util.token_tools import make_token, check_login
from django.utils import timezone


def login_view(request):
    return render(request, 'login.html')


def regist_view(request):
    return render(request, 'regist.html')


def user_view(request):
    return render(request, 'user.html')


# 用户信息
@check_login
def user_request(request, username):
    user = request.login_user
    if user.user_name != username:
        return JsonResponse({'code': 10102, 'error': '请重新登录'})
    else:
        user = User.objects.get(user_name=username)
        user_dic = {
            'user_name':user.user_name,
            'real_name':user.real_name,
            'phone':user.phone,
            'head_img':str(user.head_img),
            'address':user.address
        }
        result = {
            'code': 200,
            'user': user_dic,
            'username': username,
            'token': make_token(username),
        }
        return JsonResponse(result)


# 注册处理
def regist_request(request):
    data = json.loads(request.body)
    username = data.get('username')
    password1 = data.get('password1')
    password2 = data.get('password2')
    phone = data.get('phone')
    if password1 != password2:
        return JsonResponse({'code': 10102, 'error': '两次密码不一致!'})
    # 验证用户名密码手机号格式
    username_len, pwd_len = len(username), len(password2)
    if username_len < 6 or username_len > 11 or pwd_len < 6 or pwd_len > 11:
        return JsonResponse({'code': 10101, 'error': '用户名和密码必须6-11位!'})
    if len(phone) != 11:
        return JsonResponse({'code': 10102, 'error': '手机号格式不正确!'})
    try:
        User.objects.create(user_name=username, password1=md5_string(password2), real_name="",
                            password2=md5_string(password2), phone=phone)
    except Exception as e:
        print(e)
        return JsonResponse({'code': 10103, 'error': '用户注册失败!'})
    # 成功的响应
    result = {
        'code': 200,
        'username': username,
        'token': make_token(username),
    }

    return JsonResponse(result)


# 处理登录
def login_request(request):
    data = json.loads(request.body)
    username = data.get('username')
    password = data.get('password')
    try:
        user = User.objects.get(user_name=username)
    except Exception as e:
        return JsonResponse({"code": 10104, 'error': "用户名或密码错误！"})
    if user.password2 != md5_string(password):
        return JsonResponse({"code": 10104, 'error': "用户名或密码错误！"})
    # 成功的响应
    result = {
        'code': 200,
        'username': username,
        'token': make_token(username),
    }
    return JsonResponse(result)


# 更新用户信息
@check_login
def update_userinfo_view(request,username):
    user = request.login_user
    if user.user_name != username:
        return JsonResponse({'code': 10102, 'error': '请重新登录'})
    else:
        data = json.loads(request.body)
        print(f"data ={data}")
        real_name = data.get('realname')
        phone = data.get('phone')
        address = data.get('address')
        try:
            user = User.objects.get(user_name=username)
            print(f"user= {user}")
            user.real_name = real_name
            user.phone = phone
            user.address = address
            user.save()
            return JsonResponse({'code': 200, 'msg': "修改成功"})
        except Exception as e:
            print(e)
            return JsonResponse({'code': 10103, 'error': '用户信息修改失败!'})
